<template>
	<div>
		<!--菜单导航-->
		<el-breadcrumb separator-class="el-icon-arrow-right">
			<el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
			<el-breadcrumb-item>地址管理</el-breadcrumb-item>
			<el-breadcrumb-item>城市列表</el-breadcrumb-item>
		</el-breadcrumb>
		<!--内容卡片-->
		<el-card class="box-card">
			<!--搜索框-->
			<el-row :gutter="20">
				<el-col :span="5">
					<!--模糊搜索框-->
					<el-input placeholder="请输入查询的城市" @clear="select" clearable="" v-model="queryAddress.name"
						class="input-with-select">
						<el-button slot="append" icon="el-icon-search" @click="select"></el-button>
					</el-input>
				</el-col>

				<el-col :span="4">
					<!--添加城市 -->
					<el-button type="primary" @click="addDialogVisible=true">添加城市</el-button>
				</el-col>
			</el-row>


			<!-- 表格数据  :data代表表数据  border：带有四边框-->
			<el-table :data="address" border style="width: 90%;">
				<!-- prop：表格当前列的字段名称-->
				<el-table-column label="" type="index" width="100px" align=""></el-table-column>
				<el-table-column prop="id" align="center" label="编号"></el-table-column>
				<el-table-column prop="name" align="center" label="城市"></el-table-column>
				<el-table-column prop="parentName" align="center" label="父类城市"></el-table-column>
				<el-table-column label="操作" width="150" align="center">
					<template slot-scope="scope">
						<el-tooltip class="item" :enterable="false" effect="dark" content="删除" placement="top">
							<el-button @click="deleteAddress(scope.row.id)" type="danger" icon="el-icon-delete"
								size="mini">
							</el-button>
						</el-tooltip>
					</template>
				</el-table-column>
			</el-table>

			<!--分页
					@size-change:当每页显示的条数改变时触发函数
					@current-change：当前页码的改变触发的函数
					current-page：当前页码
					page-sizes：每页显示的条数的数组
					page-size：默认每页显示的条数
					-->
			<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
				:current-page="queryAddress.pageNum" :page-sizes="[1,3,5,10]" :page-size="queryAddress.pageSize"
				layout="total, sizes, prev, pager, next, jumper" :total="total" style="padding:25px 350px;">
			</el-pagination>


			<!--添加地址信息的对话框
				   title：对话框的提示语句
				   addDialogVisible:当前的对话框是否显示
				   width：宽带的百分比
				-->
			<el-dialog title="添加地址信息" :visible.sync="addDialogVisible" width="35%" :before-close="reset">
				<!-- 添加用户信息的表单 -->
				<el-form :model="addAddress" ref="addAddressRef" :rules="addAddresss" label-width="80px">
					  <el-form-item label="父类城市" >
					    <el-select v-model="addAddress.parentId" placeholder="请选择父类城市">
							<el-option  label="无" value="1"></el-option>
					      <el-option v-for="item in citys" :label="item.name" :value="item.id"></el-option>
					    </el-select>
					  </el-form-item>
					<el-form-item label="城市名称" prop="name" label-width="80px">
						<el-input v-model="addAddress.name" style="width: 220px;"></el-input>
					</el-form-item>
				</el-form>
				<!-- 取消和添加按钮 -->
				<span slot="footer" class="dialog-footer">
					<el-button @click="reset">取 消</el-button>&nbsp;
					<el-button type="primary" @click="addAddressForm">确 定</el-button>
				</span>
			</el-dialog>

		</el-card>

	</div>
</template>

<script>
	import qs from 'qs'
	export default {
		data() {
			return {
				//模糊分页查询对象
				queryAddress: {
					name: '',
					pageNum: 1,
					pageSize: 5
				},
				//管理员信息集合
				address: [],
				//总数居条数
				total: 0,
				addDialogVisible: false,
				addAddress: {
					name: '',
					parentId: '',
				},
				//添加数据的表单验证
				addAddresss: {
					name: [{
							required: true,
							message: '请输入城市名',
							trigger: 'blur'
						},
						{}
					],
				},
				citys: []
			}
		},
		methods: {
			//查询所有
			async selectAllCity() {
				this.citys = [];
				var res = await this.$http.post("selectAllOneAddress", null)
				if (res.data.code == 200) {
					this.$message.success(res.data.message)
					//查询数据赋值给powers
					this.citys = res.data.data;
				} else {
					this.$message.error(res.data.message)
				}
			},
			//模糊分页查询
			async getAddress() {
				this.address = [];
				var res = await this.$http.post("selectAllAddress", qs.stringify(this.queryAddress))
				if (res.data.code == 200) {
					this.$message.success(res.data.message)
					//查询数据赋值给powers
					this.address = res.data.data.records;
					//获取数据的总条数
					this.total = res.data.data.total;
				} else {
					this.$message.error(res.data.message)
				}
			},
			//改变每页显示的条数
			handleSizeChange(newpageSize) {
				this.queryAddress.pageSize = newpageSize;
				this.getAddress();
			},
			//当前页码改变时触发的函数
			handleCurrentChange(newpageNum) {
				this.queryAddress.pageNum = newpageNum;
				this.getAddress();
			},
			//根据用户名模糊查询
			select() {
				this.queryAddress.pageNum = 1;
				this.getAddress();
			},
			//添加对话框中的取消事件
			reset() {
				this.addDialogVisible = false; //关闭对话框
				this.$refs.addAddresss.resetFields();
			},
			//添加数据的确认按钮
			addAddressForm() {
				this.$refs.addAddressRef.validate(async (bo) => {
					if (bo) { //验证通过时 调用
						//调用后端的添加数据的接口，进行添加即可						
						console.log(this.addAddress)
						var res = await this.$http.post("addAddress", qs.stringify(this.addAddress))
						if (res.data.code == 200) { //成功
							this.$message.success(res.data.message)
							//更新页码数据
							this.getAddress();
							this.selectAllCity();
						} else { //失败
							this.$message.error(res.data.message)
						}
						//清空数据
						this.addAddress = {}
						this.addDialogVisible = false; //关闭对话框
					} else {
						this.$message.error("数据不合法，请检查！")
					}
				})
			},
			//删除用户数据
			deleteAddress(id) {
				this.$confirm('删除数据, 是否继续?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(async () => {
					var res = await this.$http.get("deleteAddress?id=" + id)
					if (res.data.code == 200) {
						this.$message.success(res.data.message);
					} else {
						this.$message.error(res.data.message);
					}
					this.getAddress();
				}).catch(() => {
					this.$message({
						type: 'info',
						message: '已取消删除'
					});
				});
			}
		},
		//当前页码加载前执行的代码
		mounted() {
			this.getAddress();
			this.selectAllCity();
		}
	}
</script>

<style scoped>
	.el-breadcrumb {
		font-size: 16px !important;
		margin: 15px 8px;
		margin-bottom: 20px;
	}

	/deep/.el-table__body-wrapper {
		color: coral;
	}
</style>
